#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <wchar.h> 
#include <locale.h>
#include <math.h>
#include <stdlib.h>

#include "module_display_manager.h"
#include "module_display_plugins.h"
#include "module_font_manager.h"
#include "module_font_plugins.h"

#define FONT_COLOR	0x00ffffff

#define TEST_FILE "./test.dat"

#define CMD_NULL 0
#define CMD_EXIT 1
#define CMD_TEST 2

void test(void)
{
	char *cmd;
	wchar_t *text_buf;
	int flag,cmd_code,err;
	
	flag = 1;
	while(flag)
	{	
		cmd  = malloc(1024);
		memset(cmd,	 0, 1024);
		wprintf(L"Command list:\n");
		wprintf(L"test - enter text and display on LCD.\n");
		wprintf(L"exit - exit the test program.\n");
		wprintf(L"Please enter command:");
		wscanf(L"%s",cmd);
		if 		( !(strcmp(cmd,"exit")) )	cmd_code = CMD_EXIT;
		else if ( !(strcmp(cmd,"test")) )	cmd_code = CMD_TEST;
		else cmd_code = CMD_NULL;
		free(cmd);
		switch(cmd_code)
		{
			case CMD_EXIT:
			{
				wprintf(L"EXIT\n");
				flag = 0;	
				break;
			}  
			case CMD_TEST:
			{
				text_buf = malloc(1024*4);
				memset(text_buf, 0, 1024*4);
				wprintf(L"Pleas enter text for test:");
				wscanf(L"%ls",text_buf);
				wprintf(L"%ls\n",text_buf);
				display_fill(0);
				err = font_putString(0,0,FONT_COLOR,text_buf);
				if(err)
				{
					wprintf(L"FT_putString_LCD is error!\n");
					wprintf(L"err = %d\n",err);
					flag = 0;
				}
				free(text_buf);
				break;
			}
			default:
			{
				wprintf(L"NULL\n");
				break;
			}
		}
		
	}
}

int main (int argc,char **argv)
{
    if (argc != 2)
    {
        wprintf(L"Usage : %s <font>\n",argv[0]);
        return -1;
    }
	font_var font_setting;
	display_var parmas;
	int err,font_size;

    setlocale(LC_ALL, "chs");
    
	err = display_module_init();
	if (err)
	{
		wprintf(L"display_module_init is failed!\n");
		return err;
	}
	err = display_plugin_select("lcd");
	if (err)
	{
		wprintf(L"display_plugin_select is failed!\n");
		return err;
	}
	err = display_plugin_init();
	if (err)
	{
		wprintf(L"display_plugin_init is failed!\n");
		return err;
	}	
	display_get_var(&parmas);

	wprintf(L"Please enter font size:");
	wscanf("%d",font_size);

	font_setting.filename = argv[1];
	font_setting.fontsize_width	= font_size;
	font_setting.fontsize_height = font_size;
	font_setting.border_x = parmas.res_x;
	font_setting.border_y = parmas.res_y;
	font_setting.draw_dot = display_put_pixel;
	err = font_module_init();
	if (err)
	{
		wprintf(L"font_module_init is failed!\n");
		return err;
	}
	err = font_plugin_select("FreeType");
	if (err)
	{
		wprintf(L"font_plugin_select is failed!\n");
		return err;
	}
	err = font_plugin_init(&font_setting);
	if (err)
	{
		wprintf(L"font_plugin_init is failed!\n");
		return err;
	}
	test();
	
	font_module_remove();
	display_module_remove();


	return 0;
}


